package com.lance.code.generation.service;

import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lance.code.generation.common.ConfigConstants;
import com.lance.code.generation.common.JavaBeanHandler;
import com.lance.code.generation.domain.ColumnInfo;
import com.lance.code.generation.domain.TableInfo;
import com.lance.code.generation.mapper.TablesMapper;

@Service
public class TableServiceImpl implements TableService {
	Logger logger = LoggerFactory.getLogger(getClass());
	@Autowired
	private TablesMapper tablesMapper;
	
	@Autowired
	private ConfigConstants config;
	
	@Autowired
	private JavaBeanHandler handler;

	public void run() {
		List<TableInfo> list = tablesMapper.findAll();
		for(TableInfo info: list) {
			logger.info("tableName: {}, tableComment:{}", info.getTableName(), info.getTableComment());
			handlerTableColumn(info);
		}
	}
	
	public void run(String... tableNames) {
		List<TableInfo> list = tablesMapper.findAll();
		List<String> tables = Arrays.asList(tableNames);
		for(TableInfo info: list) {
			if(tables.contains(info.getTableName())){
				logger.info("tableName: {}, tableComment:{}", info.getTableName(), info.getTableComment());
				handlerTableColumn(info);
			}
		}
	}
	
	/**
	 * 处理表字段
	 * @param info
	 * 2016年8月16日下午1:47:28
	 */
	private void handlerTableColumn(TableInfo info) {
		List<ColumnInfo> columns = tablesMapper.findColumn(info.getTableName());
		
		//创建Domain
		String domain = handler.createDomain(info, columns);
		writeFile(handler.domainPath(), handler.domainClassName(info.getTableName())+".java", domain);
		
		if(StringUtils.isBlank(config.getSql_path())){
			return;
		}
		
		if(StringUtils.isBlank(config.getSql_mapper_suffix())){
			config.setSql_mapper_suffix("-mapper");
		}
		if(StringUtils.isBlank(config.getMapper_package())){
			config.setMapper_package("mapper");
		}
		//创建Mapper
		String mapper = handler.createMapper(info);
		writeFile(handler.mapperPath(), handler.className(info.getTableName(), config.getMapper_package())+".java", mapper);
		
		//创建Mapper.xml
		String xmlMapper = handler.createXMLMapper(info, columns);
		writeFile(handler.xmlPath(), handler.className(info.getTableName(), config.getSql_mapper_suffix())+".xml", xmlMapper);
		
		
		if(StringUtils.isBlank(config.getService_package())){
			return;
		}
		//创建接口service
		String service = handler.createService(info);
		writeFile(handler.servicePath(), handler.className(info.getTableName(), config.getService_package())+".java", service);
		
		//创建接口serviceImpl
		String serviceImpl = handler.createServiceImpl(info);
		writeFile(handler.serviceImplPath(), handler.className(info.getTableName(), handler.servieImplName())+".java", serviceImpl);
	}
	
	/**
	 * 写文件
	 * @param dir
	 * @param fileName
	 * @param content
	 * 2016年8月16日下午2:50:55
	 */
	private void writeFile(String dir, String fileName, String content){
		File dic = new File(dir);
		if(!dic.exists()){
			dic.mkdirs();
		}
		
		try {
			Path file = Paths.get(dir+"/"+fileName);
			Files.deleteIfExists(file);
			file = Files.createFile(file);
			
			BufferedWriter writer = Files.newBufferedWriter(file, Charset.forName("utf-8"));
			writer.write(content);
			writer.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
